package edu.gatech.doi.spamframework.mapping;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class AddComment {

	public void addComment(String dir,String ddir)
	{
		File originaldir=new File(dir);
		
		File[] filelist=originaldir.listFiles();
		    
	    for(int j=0;j<filelist.length;j++)
	    {
	    			
	    		
				FileWriter writer = null;
			    BufferedWriter bw=null;
			    File file=filelist[j];
			    
			    String newfilename=ddir+file.getName();
			   
				try {
					 File newfile=new File(newfilename);
					    if(newfile.exists())
					    {
					    	newfile.delete();
					    	newfile.createNewFile();
					    }
					    else
					    {
					    	newfile.createNewFile();
					    }
					writer =new FileWriter(newfilename, true);
					 bw=new BufferedWriter(writer);
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
			    bw=new BufferedWriter(writer);
				//File input = new File(filename);
				try {
					BufferedReader in
					   = new BufferedReader(new FileReader(file.getAbsolutePath()));
					String line;
					String regx="*<*>*";
					boolean isbody=false;
					while((line=in.readLine())!=null)
					{
						String newline;
						if(line.matches(regx))
						{
							isbody=true;
						}
						if(isbody==false)
						{
							 newline="<!--"+line+"-->";
							 bw.write(newline);
				    		 bw.newLine();
				    		 bw.flush();
						}
						else
						{
							newline=line;
							 bw.write(newline);
				    		 bw.newLine();
				    		 bw.flush();
						}
						
					}
					bw.close();
			    	writer.close();
					
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				 
				writer=null;
				bw=null;
	    }

	}
	
	public static void main(String args[])
	{
		AddComment ac=new AddComment();
		String dir=args[0];
		String ddir=args[1];
		ac.addComment(dir, ddir);
	}
}
